from flask import Blueprint, request, jsonify
import json

bp_cate = Blueprint('cate', __name__)

from db import db1


@bp_cate.route('/cateM', methods=['POST', 'GET'])
def cateM():
    if request.method == "POST":
        # 获取用户输入name
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        # 构造sql评句
        sql = "insert into cate(name) values('%s')" % (data['name'])
        # pymysql操作
        db1.insert(sql)
        db1.close()
        # 返回结果
        return jsonify({'code': 200})
    else:
        sql = "select * from cate"
        print(sql)
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})
